﻿using System.Threading.Tasks;
using JTTD.Domain.Dtos;
using JTTD.Domain.IService;
using Microsoft.AspNetCore.Mvc;
using JTTD.Domain.Dtos.Integral;
using System.Net;
using JTTD.Domain;
using JTTD.Domain.Dtos.Integral.Response;
using JTTD.Domain.Dtos.Integral.Reuqest;
using JTTD.Common.Enum;
using JTTD.Common.Attribute;
using System;
using JTTD.Extensions;
using JTTD.Domain.Dtos.Integral.Request;
using JTTD.Common.Helper;

namespace JTTD.Controllers
{
    /// <summary>
    /// 积分模块
    /// </summary>
    [Route("api/integrals")]
    [ApiController]
    public class IntegralController : BaseController
    {
        private readonly IIntegralService _integralService;
        /// <summary>
        /// 积分模块
        /// </summary>
        /// <param name="integralService"></param>
        public IntegralController(IIntegralService integralService)
        {
            _integralService = integralService;
        }

        /// <summary>
        /// 分页获取账单核算积分申请(通用)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("applys")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<PageResult<IntegralApplyResponseDto>>))]
        public async Task<IActionResult> GetApplyList([FromQuery]SearchIntegralApplyRequestDto dto)
        {
            int? userId = null;
            if (dto.SourceType == 1)
            {
                userId = UserId;
            }
            var result = await _integralService.GetApplyList(dto, userId);
            return Ok(result);
        }

        /// <summary>
        /// 分页获取用户积分收支记录(通用)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("records")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<PageResult<IntrgralRecordResponseDto>>))]
        public async Task<IActionResult> GetRecordPageList([FromQuery]SearchIntrgralRecordRequestDto dto)
        {
            var result= await _integralService.GetRecordPageList(dto);
            return Ok(result);
        }

        /// <summary>
        /// 提交账单核算积分申请（微信公众号）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("apply")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Ordinary)]
        public async Task<IActionResult> CrateApply([FromBody]CreateApplyRequestDto dto)
        {
            var result = await _integralService.CrateApply(UserId,dto.FileUrl,dto.Amount,dto.Phone);
            return Ok(result);
        }

        /// <summary>
        /// 审核账单核算积分申请（后台管理员）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("applys/examine")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> ExamineApply([FromBody]ExamineApplyRequestDto dto)
        {
            var result = await _integralService.ExamineApply(dto.ApplyId,dto.ApplyState,dto.Reason);
            return Ok(result);
        }

        /// <summary>
        /// 赠送指定用户积分（后台管理员）
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="amount">积分金额</param>
        /// <returns></returns>
        [HttpPut]
        [Route("{userId}/{amount}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> GiveIntegral([FromRoute]int userId, [FromRoute]decimal amount)
        {
            var result = await _integralService.GiveIntegral(userId, amount);
            return Ok(result);
        }
        
        /// <summary>
        /// 扣减指定用户积分（后台管理员）
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="amount">积分金额</param>
        /// <returns></returns>
        [HttpGet]
        [Route("remove")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> RemoveIntegral(int userId, decimal amount)
        {
            var result = await _integralService.RemoveIntegral(userId, amount);
            return Ok(result);
        }
        
        
        /// <summary>
        /// 查询积分明显报告
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("report")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<PageResult<IntrgralReportResponseDto>>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> PageIntrgralReport([FromBody]SearchIntrgralReportRequestDto dto) {

            var data = await _integralService.PageIntrgralReport(dto);
            return Ok(data);
        }

        /// <summary>
        /// 导出积分明细报告
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("exportIntrgralReport")]
        [UnAuthorize]
        public async Task<IActionResult> GetIntrgralReportExportList([FromQuery]SearchIntrgralReportRequestDto dto )
        {
            var list = await _integralService.GetIntrgralReportExportList(dto);
            byte[] buff = ExcelHelper.Export(list);
            Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
            return File(buff, "application/vnd.ms-excel", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx");
        }



        [HttpGet]
        [Route("Fix")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UnAuthorize]
        public async Task<IActionResult> Fix()
        {
            var result = await _integralService.Fix();
            return Ok(result);
        }
    }
}
